import originAxios from 'axios';
import React from 'react';
import ReactDOM from 'react-dom';
import { Spin } from 'antd';


export default function request(option) {
  return new Promise((resolve, reject) => {
    const { isLoading = false } = option;

    // 1.创建axios的实例

    const instance = originAxios.create({

      baseURL: 'http://123.207.32.32:9001',
      timeout: 10000,
      
    });
    // 配置请求和响应拦截
    instance.interceptors.request.use(
      config => {
        if (isLoading) {
          const dom = document.createElement('div');
          dom.setAttribute('id', 'loading');
          document.body.appendChild(dom);
          ReactDOM.render(<Spin tip='加载中' size='large' />, dom);
        }
        return config;
      },
      err => {
        return err;
      }
    );

    instance.interceptors.response.use(
      response => {
        if (isLoading) {
          document.body.removeChild(document.getElementById('loading'));
        }
        return response.data;
      },
      err => {
        if (isLoading) {
          document.body.removeChild(document.getElementById('loading'));
        }
        return err;
      }
    );

    // 2.传入对象进行网络请求
    instance(option)
      .then(res => {
        resolve(res);
      })
      .catch(err => {
        reject(err);
      });
  });
}
// 
// request({
//   url:'/banner'
// })

// function abc(option) {
//   return (resolve, reject) => {
//     if (true) {
//       resolve()
//     } else {

//       reject()
//     }
//   }
// }
// abc({}).then().catch()
